package com.repository;

import com.entity.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface ArticleRepository extends JpaRepository<Article,Integer>, JpaSpecificationExecutor<Article> {

    long count();

    long countByUserId(Integer userId);

    long countByUserIdAndIsPublic(Integer userId,Integer isPublic);

    long countByTopicId(Integer topicId);

    long countByTitleLike(String title);

    long countByIsPublicAndTopicIdIsNotNull(Integer isPublic);

    List<Article> findByIsRecommend(@Param("isRecommend") Integer isRecommend);

    Page<Article> findAllByUserId(Integer userId,Pageable page);

    Page<Article> findAllByUserIdAndIsPublic(Integer userId,Integer isPublic ,Pageable page);

    Page<Article> findAllByTopicId(Integer topicId,Pageable page);

    Page<Article> findAllByTitleLike(String title,Pageable page);

    Page<Article> findAllByIsPublicAndTopicIdIsNotNull(Integer isPublic ,Pageable page);

    @Modifying
    @Query(nativeQuery = true, value = "UPDATE article a SET topic_id =:topicId WHERE id =:id")
    void updateTopicIdById(@Param("id") Integer id,@Param("topicId") Integer topicId);

}
